package sisi.currency;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sisi.General;

/*
*
* CurrencyWindow.java - Controller class for <i>Currency window</i>. 
* @author Pedro Gonzalez pedro@sisisoftware.it
* @author Claudio Pelosi claudio@sisisoftware.it
* @author SISI Software S.n.c. - www.sisisoftware.it
* @version 1.0, 07/07/2014
* 
*/


@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class CurrencyWindow extends Window implements 
				org.zkoss.zk.ui.ext.AfterCompose {
//window
private Window windowCurrency, windowEditCurrency;
private Textbox tbCerca;
private Listbox boxCurrency;
private List lista_Currency;
private boolean lNuovo;
private Toolbarbutton buttonSeleziona;
private ListModelList listModelList;
private Integer idSelezionato = 0;

protected Map<String, Object> args;
private Object CurrencyItem;
private Currency modCurrency;
// edit
private Intbox id;
private Textbox curr;
private Textbox currdesc;

		public void onCreate$windowCurrency(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N": tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		lista_Currency = new CurrencyController().getList(null);
		listModelList.addAll(lista_Currency);
		boxCurrency.setModel(listModelList);
		boxCurrency.setItemRenderer(new CurrencyItemRenderer());

		if (idSelezionato!=null && idSelezionato!=0){
		for (int i = 0; i < lista_Currency.size(); i++) {
			Currency record = (Currency)lista_Currency.get(i);
			if (record.getId()==idSelezionato.intValue()){
				boxCurrency.setSelectedIndex(i);
				break;
			}
		}
	}

}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}

	public void onClick$buttonCerca(Event event) {
		cerca();
	}


	private void cerca(){
		listModelList = new ListModelList();
		lista_Currency = new CurrencyController().getList(tbCerca.getValue());
		listModelList.addAll(lista_Currency);
		boxCurrency.setModel(listModelList);
		boxCurrency.setItemRenderer(new CurrencyItemRenderer());
	}

	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}

	public void onClick$buttonSeleziona(Event event) {

		seleziona();

}

	public void seleziona() {
		if (boxCurrency.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oCurrency = boxCurrency.getSelectedItem().getAttribute("rigaCurrency");
			String codiceSelezionato = ""+((Currency) oCurrency).getId();
			Sessions.getCurrent().setAttribute("codiceCurrencySelezionato", codiceSelezionato);
			windowCurrency.detach();
		}
	}

	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}

	private void modifica(boolean nuovo){
		try {

			int nIndex = boxCurrency.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a row to edit", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxCurrency", boxCurrency);
				Window finestraEditCurrency = (Window) Executions.createComponents("/editCurrency.zul", null, map);
				finestraEditCurrency.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}

	public void onClick$buttonCancella(Event event) {
	// TODO Auto-generated method stub

	}

	public class CurrencyItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Currency) data).getCurr()).setParent(li);
			new Listcell(""+((Currency) data).getCurrdesc()).setParent(li);
			li.setAttribute("rigaCurrency", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}


// EDIT: NEW/MODIFY
	public void onCreate$windowEditCurrency(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();

		boxCurrency = (Listbox) args.get("boxCurrency");

		if (args.containsKey("lNuovo")) {
			lNuovo = true;
		} else {
			lNuovo = false;
		}

		if (!lNuovo) {
			CurrencyItem = boxCurrency.getSelectedItem().getAttribute("rigaCurrency");
			modCurrency = new CurrencyController().refresh((Currency) CurrencyItem);
			CurrencyItem = modCurrency;
			windowEditCurrency.setTitle("Edit Currency");
			//categoryname.setFocus(true);
			//id.setValue(modCategorie.getCategoryid());
			//categoryname.setValue(modCategorie.getCategoryname());
			//description.setValue(modCategorie.getDescription());
			id.setValue(modCurrency.getId());
			curr.setValue(modCurrency.getCurr());
			currdesc.setValue(modCurrency.getCurrdesc());
		} else {
			windowEditCurrency.setTitle("New Currency");
			//categoryname.setFocus(true);
		}

	}

	public void onClick$btnAnnulla(Event event){
		windowEditCurrency.detach();
	}

	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modCurrency = new Currency();
		} else {

		}
		String testoMsg = "";

		modCurrency.setId(id.getValue());
		modCurrency.setCurr(General.toUpperAncheNull(curr.getValue()));
		modCurrency.setCurrdesc(General.toUpperAncheNull(currdesc.getValue()));

		if (lNuovo){
			new CurrencyController().add(modCurrency);
			testoMsg = "New Currency: "+ General.trimNull( modCurrency.getCurrdesc() );
		} else {
			new CurrencyController().update(modCurrency);
			testoMsg = "Updating Currency: "+ General.trimNull( modCurrency.getCurrdesc() );
		}

		General.showNotify( testoMsg, windowEditCurrency);

		windowEditCurrency.detach();

		// aggiorna listbox

		if (lNuovo){
			ListModelList lml = (ListModelList) boxCurrency.getListModel();
			lml.add(modCurrency);
			boxCurrency.setSelectedIndex(lml.indexOf(modCurrency));
		} else {
			ListModelList lml = (ListModelList) boxCurrency.getListModel();
			Object oCurrencyItem = boxCurrency.getSelectedItem().getAttribute("rigaCurrency");
			if (lml.indexOf(oCurrencyItem) == -1) {
				// non trovato... quindi non lo aggiorno
			} else {
				lml.set(lml.indexOf(oCurrencyItem), modCurrency);
			}
		}
	}

}
//
// fine
